package cn.iocoder.yudao.module.wms.controller.admin.inventory.stock;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.wms.controller.admin.inventory.stock.vo.InventoryStockPageReqVO;
import cn.iocoder.yudao.module.wms.controller.admin.inventory.stock.vo.InventoryStockReqVO;
import cn.iocoder.yudao.module.wms.controller.admin.inventory.stock.vo.InventoryStockRespVO;
import cn.iocoder.yudao.module.wms.dal.dataobject.inventory.stock.InventoryStockDO;
import cn.iocoder.yudao.module.wms.service.inventory.stock.InventoryStockService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 存货现存量")
@RestController
@RequestMapping("/wms/inventory/stock")
@Validated
public class InventoryStockController {

    @Resource
    private InventoryStockService inventoryStockService;

    @GetMapping("/get")
    @Operation(summary = "获得存货现存量档案")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:inventory:query')")
    public CommonResult<InventoryStockRespVO> getInventoryStock(@RequestParam("id") Long id) {
        InventoryStockDO stock = inventoryStockService.getInventoryStock(id);
        return success(BeanUtils.toBean(stock, InventoryStockRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得存货现存量档案分页")
    @PreAuthorize("@ss.hasPermission('wms:inventory:query')")
    public CommonResult<PageResult<InventoryStockRespVO>> getInventoryStockPage(@Valid InventoryStockPageReqVO pageReqVO) {
        //获取存货档案分页
        PageResult<InventoryStockDO> pageResult = inventoryStockService.getInventoryStockPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, InventoryStockRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得存货现存量精简列表", description = "主要用于前端的下拉选项")
    @Parameter(name = "query", description = "编码或名称", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:inventory:query')")
    public CommonResult<List<InventoryStockRespVO>> getInventoryStockSimpleList(@RequestParam("query") String query) {
        //数据会很多,必须添加筛选条件
        List<InventoryStockDO> list = inventoryStockService.getInventoryStockSimpleList(query);
        return success(BeanUtils.toBean(list, InventoryStockRespVO.class));
    }

    @GetMapping("/availableAmount")
    @Operation(summary = "获得可用量")
    @PreAuthorize("@ss.hasPermission('wms:inventory:query')")
    public CommonResult<Double> getInventoryStockSimpleList(@Valid InventoryStockReqVO inventoryStockReqVO) {
        Double amount = inventoryStockService.getInventoryAvailableAmount(inventoryStockReqVO);
        return success(amount);
    }

}